package com.hl.yyx.security.component;

import cn.hutool.core.util.StrUtil;
import com.hl.yyx.common.util.JwtTokenUtil;
import com.hl.yyx.common.util.RequestHolder;
import com.hl.yyx.modules.ums.mapper.UmsAdminMapper;
import com.hl.yyx.modules.ums.model.UmsAdmin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * JWT登录授权过滤器
 */
@Slf4j
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private UmsAdminMapper umsAdminMapper;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;


    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        try {
            log.info("进入LoginFilter");
            // 拿到jwt令牌
            String jwt = request.getHeader(tokenHeader);
            //判断是否存在 判断开头是否加了tokenHead Bearer
            if (!StrUtil.isBlank(jwt) && jwt.startsWith(tokenHead)) {
                // 解密
                jwt = jwt.substring(tokenHead.length());
                String userName = jwtTokenUtil.getUserNameFromToken(jwt);

                if (!StrUtil.isBlank(userName)) {
                    // 从服务器中查询
                    UserDetails userDetails = userDetailsService.loadUserByUsername(userName);
                    if (userDetails != null) {
                        UmsAdmin admin = umsAdminMapper.selectByAccount(userName);
                        RequestHolder.add(request);
                        RequestHolder.add(admin);
                        // 生成springSecurity的通过认证标识
                        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
                        authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));

                        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
                    }
                }
            }
            filterChain.doFilter(request, response);
        } finally {
            RequestHolder.remove();
        }

    }
}
